package utils;

import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

/**
 * @author Ajie
 * @date 2020-06-18
 * @function
 */
public class LogSplitUtil {
    public static void main(String[] args) {
        logger.info("main: ");
        e("taggg","12345678");
    }

    private static final Logger logger = Logger.getLogger(LogSplitUtil.class);
    /**
     * 截断输出日志
     *
     * @param msg
     */
    public static void e(String tag, String msg) {
        if (tag == null || tag.length() == 0
                || msg == null || msg.length() == 0)
            return;

        int segmentSize = 3 * 1024;
//        int segmentSize = 3;
        long length = msg.length();
        if (length <= segmentSize) {// 长度小于等于限制直接打印
            logger.info( msg);
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(msg);

            while (sb.toString().length() > segmentSize) {// 循环分段打印日志
                String logContent = sb.substring(0, segmentSize);
                sb = sb.replace(0, segmentSize, "");
                logger.info( logContent);
            }
            logger.info(sb.toString());// 打印剩余日志
        }
    }
}
